App.controller("activityCheckCtrl",function($scope, $rootScope, $location, $http, $compile, $stateParams, $timeout, path, publicService) {
	
	$scope.search = {};
	var alertModel = {};	//alert模态框
	var checkArr = new Array();   //批量通过或者拒绝审核状态
	var recordState = 1;        //记录点击的是全选还是单个的生成几个（1是单个的，2 是全选
	var checkState = [
		                 {
			                value: "5",
			      			text: "请选择",
				         },
			             {
			                value: "0",
			      			text: "待审核",
					     },
			             {
			                value: "2",
			      			text: "未通过",
					     },
			                 
			          ];
	
	$("#checkState").kendoDropDownList({
	    dataTextField: "text",
	    dataValueField: "value",
	    dataSource: checkState
	    });
	//表格对象
	$scope.gridOptions = {
			dataSource : {
				transport : {
					read : {
						url : path+"/server/selectActivityAuditPage",
						type : 'POST',
						dataType : "json",
						contentType : "application/json"
					},
					parameterMap : function(options, operation) {
						if (operation == "read") {
							var sort = [{dir:"desc",field:"create_time"}];
							if(options.sort!=null && options.sort.length>0 ){
								if(options.sort[0].field=="create_time"){
									options.sort[0].field="create_time";
								}else{
									options.sort[0].field="create_time";
								}
								
								sort=options.sort;
							}
							var parameter = {
								page : options.page, //当前页
								pageSize : options.pageSize,
								param:{
									name:$scope.search.activity_name,//活动名称
									creater:$scope.search.creat_name,//创建者
									create_time:$('#startTime').val(),//创建时间(开始时间)
									end_time:$('#endTime').val(), //结束时间
									putaway:$("#upDownFramer").val(),//上下架
									audit_state:$('#checkState').val(),
									sort:sort
								}
							};
							console.log('查询参数',parameter);
							return kendo.stringify(parameter);
						}
					},
				},
				pageSize : 10,
				serverPaging : true,
				serverSorting: true,
				schema : {
					data : function(d) {
						return d.data;
					},
					total : function(d) {
						return d.total; //总条数
					}
				},
			},
			pageable : {  //分页
				pageSizes: [10, 20, 50, 100],  //每页显示记录数
			  },  
			sortable: {   //排序
				//allowUnsort: false,  //允许无序
			    mode: "multiple"     //排序模式：single，multiple
			  },
			editable: false,  //true：单个编辑；popup：弹出新页面编辑
			dataBound: function () {   //序号
		        var rows = this.items();
		        var page = this.pager.page() - 1;
		        var pagesize = this.pager.pageSize();
		        $(rows).each(function () {
		            var index = $(this).index() + 1 + page * pagesize;
		            var rowLabel = $(this).find(".row-number");
		            $(rowLabel).html(index);
		        });
		    },
			columns : [
					{    
						title : "序号",
						field : "id", 
						width : "50px",
						template:function(e){
						var html = "<input type='checkbox' class='recordClass' value="+e.id+" data-state="+e.state+" />"+"<span class='row-number subTitle'></span>";
						return html;
						}
					},
					{
						field : "name",         
						title : "活动名称",     
						width : "100px"
					},
					{
						field : "fname",         
						title : "创建者",     
						width : "100px"
					},
					{
						field : "create_time",         
						title : "创建日期",     
						width : "100px"
					},
					{
						field : "start_time",         
						title : "活动周期",     
						width : "200px",
						template:function(e){
							return e.start_time + "---" + e.end_time;
						}
					},
					{
						field : "shop_num",         
						title : "专卖店参与量",     
						width : "100px"
					},
					{
						field : "audit_state",         
						title : "状态",     
						width : "100px",
						values:checkState
					},
					{
						field : "",         
						title : "操作",     
						width : "100px",
						template: function(e){
							var	template_html = "";
							console.log(e.audit_state);
							if(userInfo.user_level==2){  //二级
								if(e.audit_state == 0){  //表示未开始  可以选择拒绝或者成功
									template_html ='<div class="operation k-state-default " ng-click="lookDetails(\''+e.id+'\')">查看</div>'
								}else if(e.audit_state == 1){  //进行时
									template_html ='<div class="operation k-state-default " ng-click="lookDetails(\''+e.id+'\')">查看</div>'
								}else if(e.audit_state == 2){  //结束
									template_html = '<div class="operation k-state-default splitButtonTwoc" ng-click="editDetails(\''+e.id+'\')">编辑</div>'
										+'<div class="operation k-state-default splitButtonTwoc" ng-click="repulseCheck(\''+e.id+'\',0)">重新提交审核</div>';
								}
					    	}else if(userInfo.user_level==3){  //三级
								if(e.audit_state == 0){  //表示未开始  可以选择拒绝或者成功
									template_html ='<div class="operation k-state-default " ng-click="lookDetails(\''+e.id+'\')">查看</div>'
									+'<div class="operation k-state-default splitButtonTwoc" ng-click="repulseCheck(\''+e.id+'\',2)">拒绝</div>'
									+'<div class="operation k-state-default splitButtonTwoc" ng-click="repulseCheck(\''+e.id+'\',1)">通过</div>';
								}else if(e.audit_state == 1||e.audit_state ==2){
									template_html ='<div class="operation k-state-default " ng-click="lookDetails(\''+e.id+'\')">查看</div>'
								}
					    	}
					    	return template_html;
					    },
					}
			]
	};
	
	//查询
	$scope.search = function(){
		$scope.grid.dataSource.page(1);
	}
	
	//全选，取消
	$scope.chooseAll = function(className,flag){
		publicService.chooseAllByClassName(className,flag);
		console.log(className);
		var checkBoxArr = $("."+className);
	};
	
	//审核
	$scope.check = function(flag){
		var paramList = [];
		var idArray = publicService.getChooseValueByClassName("recordClass");
		
		$.each(idArray, function(index, OneObj){
			var item = {
					id: OneObj,
					audit_state: flag
			};
			paramList.push(item);
		});
		
		if(paramList.length==0){
			alertModel.pannel = jqueryAlert({
				content: "请选择要审核的活动！",
				modal: true
			});
			return;
		}
		
		$http({
			   url:	path + "/server/updateActivity",	
			   method: 'POST',
			   data: angular.toJson(paramList)
		}).success(function(data){
			if(data.code==0){
				alertModel.pannel = jqueryAlert({
					content: "审核成功！",
					modal: true
				});
			}else{
				alertModel.pannel = jqueryAlert({
					content: "审核失败！",
					modal: true
				});
			}
		}).error(function(data){
			console.warn(data);   
		})
	};
	//批量处理
	$scope.batchPass = function(id){
		checkArr.splice(0,checkArr.length);  //清空数组
		$.each($(".recordClass"),function(index,objs){
			if(objs.checked){
				 if(objs.dataset.state == 0){
						var objs1 = {};
						objs1.id =parseInt(objs.value);   //id 
						objs1.state =id;  //是拒绝还是通过
						checkArr.push(objs1);  //id
				 }
			}
		});
		if(checkArr.length>0){
			$http({
				   url:	path + "/server/updateActivityStateOne",	
				   method: 'POST',
				   data:angular.toJson(checkArr)
			}).success(function(data){
				if(data.code==0){
					alert("操作成功");
					$scope.search();
				}else{
					alert("操作成功");
					$scope.search();
				}
			}).error(function(data){
				console.warn(data);   
			})
		}else{
			alert("请先勾选产品！");
		};
	};
	//单个的通过和拒绝 and 重新发布
	$scope.repulseCheck = function(id,num){
		var reple = {
				id:id,
				audit_state:num
		};
		$http({
			   url:	path + "/server/updateActivityStateOne",	
			   method: 'POST',
			   data:angular.toJson(reple)
		}).success(function(data){
			if(data.code==0){
				alert("操作成功");
				$scope.search();
			}else{
				alert("操作成功");
				$scope.search();
			}
		}).error(function(data){
			console.warn(data);   
		})
	}
	//查看
	$scope.lookDetails = function(id){
		var params = {method:"look2",brandId:id,backUrl:"activityCheck"};
		$location.path("/addUnionOrderActivity/"+angular.toJson(params));
	}
	//编辑
	$scope.editDetails = function(id){
		var params = {method:"edit",brandId:id,backUrl:"activityCheck"};
		$location.path("/addUnionOrderActivity/"+angular.toJson(params));
	}
	
	publicService.initDateTime("startTime");	//开始时间
	publicService.initDateTime("endTime");		//结束时间
	
	
	
});